#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void intiboard(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	for (i = 0;i < rows;i++)
	{
		int j = 0;
		for (j = 0;j < cols;j++)
		{
			arr[i][j] = set;
		}
	}
}

void showboard(char arr[ROWS][COLS], int rows, int cols)
{
	printf("##############sweep mine#############\n");
	int i = 0;
	for (i = 0;i <= rows-2;i++)
	{
		printf("%d", i);
	}
	printf("\n");
	for (i = 1;i <= rows - 2;i++)
	{
		int j = 0;
		printf("%d", i);
		for (j = 1;j <= cols - 2;j++)
		{
			printf("%c", arr[i][j]);
		}
		printf("\n");
	}
	printf("##############sweep mine#############\n");

}

void setmine(char arr[ROWS][COLS], int row, int col)
{
	int x, y;
    int i=0;
	
	while(i < EASY_MINE)
	{   x = rand() % row + 1;
	    y = rand() % row + 1;
		if (arr[x][y] == '0')
		{
			arr[x][y] = '1';
			i++;
		}
	}

}


int countmine(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] +
		mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y+1]-8*'0';
}


void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col)
{
	int x, y;
	int win = 0;
	while(win<row*col- EASY_MINE)
	{
		printf("please input coordinate:>\n");
		scanf("%d%d", &x, &y);
		if (x <= rows - 2 && x >= 1 && y <= rows - 2 && y >= 1)
		{
			if (mine[x][y] == '1')
			{
				printf("game over\n");
				showboard(mine, ROWS, COLS);
				break;

			}
			if (mine[x][y] == '0')
			{
				win++;
				show[x][y] = countmine(mine,x,y) + '0';//2+'0'='2'
				showboard(show, ROWS, COLS);

			}
		}
		else
		{
			printf("input error,please enter again\n");

		}
	}
	if (win == row * col - EASY_MINE)
	{
		printf("!!!!!!!!!!!!!!!!!\n");
		printf("you are winner!!!\n");
		printf("!!!!!!!!!!!!!!!!!\n");
	}
}